﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using Portal.Workflow.Model;
using Portal.Core;
using dkSoft.SqlDatabase;

namespace Portal.Workflow.Utilities
{
    public class DatabaseServiceBll
    {
        public static string GetHttpRoot()
        {
            string HttpRoot =
                "       http://" + System.Web.HttpContext.Current.Request.Url.DnsSafeHost + ":" + System.Web.HttpContext.Current.Request.Url.Port + System.Web.HttpContext.Current.Request.ApplicationPath;
            return HttpRoot;
        }

        public static string GetPathRoot()
        {
            string PathRoot = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
            return PathRoot;
        }

        public static string GetConnectionString(WsInParamCls oWsInParamCls)
        {
            return oWsInParamCls.ConnectionString;
        }


        public static IDatabase CreateDatabaseService(WsInParamCls oWsInParamCls)
        {
            //doc web.config 
            return new SqlDatabase(GetConnectionString(oWsInParamCls));
        }



        public static string ProcessSearchKeyword(string Keyword)
        {
            if (String.IsNullOrEmpty(Keyword)) Keyword = "";
            Keyword = Keyword.Replace("'", "");
            Keyword = Keyword.Replace(",", "");
            Keyword = Keyword.Replace("%", "");
            return Keyword;
        }

        public static string GetString(DataRow dr, string KeyField)
        {
            if (dr[KeyField] != DBNull.Value)
            {
                return (string)dr[KeyField];
            }
            else
            {
                return null;
            }
        }
        public static DateTime GetDateTime(DataRow dr, string KeyField)
        {
            if (dr[KeyField] != DBNull.Value)
            {
                return DateTime.Parse(dr[KeyField].ToString());
            }
            else
            {
                return new System.DateTime();
            }
        }
        public static decimal GetDecimal(DataRow dr, string KeyField)
        {
            if (dr[KeyField] != DBNull.Value)
            {
                return (decimal)dr[KeyField];
            }
            else
            {
                return 0;
            }
        }

        public static int GetInt(DataRow dr, string KeyField)
        {
            if (dr[KeyField] != DBNull.Value)
            {
                return (int)dr[KeyField];
            }
            else
            {
                return 0;
            }
        }
    }
}
